# -*- coding: utf-8 -*-
#*******************************************************************************
#   
#   http://www.tuku.cc/comic/\d+/ 解析器
#
#*******************************************************************************
from base import *

tag='tuku'

#---------------------------- 验证模块 ----------------------------

def url_judgement(url):
    if re.match(r'http://www.tuku.cc/comic/\d+/',url):
        return valid_check(url)
    return False

def valid_check(url):
    print('{} catch {}'.format(tag,url))
    content=request_for(url,common_headers)
    if content:
        content=content.decode('utf-8')
        if re.search(r"http://tkres.tukucc.com/skins/images/bz002.jpg",content):
            return False
        return get_loader(url)
    return False

def get_loader(url):
    return TukuccLoader(url,TukuccPageParser,TukuccVolParser)

#---------------------------- 下载器 ----------------------------

class TukuccLoader(ComicLoader):
    def __init__(self,url,comic_page_parser,comic_vol_parser):
        super().__init__(url,comic_page_parser,comic_vol_parser)

#---------------------------- 解析器 ----------------------------

class TukuccPageParser(ComicPageParser):
    def __init__(self,comic):
        super().__init__(comic)
    
    def wrok(self,content):
        site_prefix='http://www.tukucc.com'
        # title
        self.comic.title=re.compile(r'<h1>(.*?)</h1>',re.S|re.M).findall(content)[0]
        # description
        self.comci.description=re.compile(r'<p>(.*?)</p>',re.S|re.M).findall(content)[0]
        # cover
        self.comic.cover=re.compile(r'<div class="thumbimg"><img src="(.*?)" border="0" alt=',re.S).findall(content)[0]
        # vols
        urls_content=re.compile(r'name="charpter_content" class="content">(.*?)<div class="clear">',re.S|re.M).findall(content)[0]
        urls=re.compile(r'><a href="(.*?)">(.*?)<i> </i>',re.S|re.M).findall(urls_content)
        for (url,title) in urls:
            self.comic.vols.append(InfoVol(site_prefix+url,title))

class TukuccVolParser(ComicVolParser):
    def __init__(self):
        super().__init__()

    def work(self,content):
        js_content=re.compile(r'\}\((.*?)\)\)').findall(content)[0]
        enc_content=re.compile(r'\'(.*)\',').findall(js_content)[0]
        digits=re.compile(r',(\d+)').findall(js_content)
        a,c=int(digits[0]),int(digits[1])
        key_arr=re.compile(r",'(.*?)'.s",re.S|re.M).findall(js_content)[0].split('|')
        c,dec_arr=c-1,{}
        while c>=0:
            idx=e_func(c,a)
            dec_arr[idx]=key_arr[c] or idx
            c-=1
        def rep(m):
            enc=m.group()
            dec=None
            try:
                dec=dec_arr[enc]
            except:
                dec=enc
            return dec
        enc_content=re.subn(r'\w+',rep,enc_content)[0].replace('\\','')
        infos=re.compile(r"var pic_url='(.*?)';var tpf=(.*?);var pages=(.*?);var pid=(.*?);var pic_extname='(.*?)'").findall(enc_content)[0]
        pic_url,tpf,pages,pid,pic_extname=infos[0],int(infos[1]),int(infos[2]),infos[3],infos[4]
        for i in range(1,pages+1):
            url='http://tkpic.tukucc.com/'+pid+'/'+pic_url+'/'+zero_prefix(i,tpf+1)+'.'+pic_extname
            self.pic_urls.append((url,zero_prefix(self.pic_index)))
            self.pic_index+=1

def to36(val):
    return toRadix(val,36)

def e_func(c,a):
    c,a=int(c),int(a)
    res=None
    if c<a: 
        res=''
    else:   
        res=e_func(int(c/a),a)
    c=c%a
    if c>35: 
        res+=chr(c+29)
    else: 
        res+=to36(c)
    return res